/*
 * Copyright (c) 2015. For Intelligent Group.
 */

package com.intelligent.ispc.r.analysis.grr;

import com.intelligent.ispc.common.utils.ApplicationException;
import com.intelligent.ispc.common.utils.ExceptionMessages;
import com.intelligent.ispc.common.utils.ResourceBundleUtils;
import com.intelligent.ispc.core.dto.GrrAnalysisDataDto;
import com.intelligent.ispc.core.dto.GrrParamDto;
import com.intelligent.ispc.r.analysis.RCommand;
import com.intelligent.ispc.r.utils.REnConnector;
import com.intelligent.ispc.r.utils.RUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by Peter on 2015/12/10.
 */
public class GrrChartPlotCommand implements RCommand {
    private static Logger logger = LoggerFactory.getLogger(GrrChartPlotCommand.class);

    private String scriptPath = "";
    private GrrParamDto grrParamDto;
    private GrrConfiguration configuration;
    private GrrAnalysisDataDto analysisParam;

    private double operator = 0D;
    private double parts = 0D;
    private double trial = 0D;
    private double sigma = 0D;
    private double tole = 0D;

    /**
     * @param scriptPath the file path
     */
    public GrrChartPlotCommand(String scriptPath) {
        this.scriptPath = scriptPath;
    }

    protected void buildScript() {
        try {
            REnConnector con = RUtils.getInstance().getConnector();

            operator = Double.parseDouble(grrParamDto.getOperatorValue());
            parts = Double.parseDouble(grrParamDto.getPartValue());
            trial = Double.parseDouble(grrParamDto.getTrialValue());
            sigma = Double.parseDouble(grrParamDto.getSigma());

            con.setInput("x", analysisParam.getAnalysisData());
            con.setInput("bitNum", grrParamDto.getDigitNum());
            //operators
            con.setInput("k", operator);
            //parts
            con.setInput("n", parts);
            //trial
            con.setInput("r", trial);

            con.setInput("sig", sigma);
            con.setInput("tole", tole);
            con.execEval("source(\"" + scriptPath + "\")");
            while (con.isActive()) {
                con.disconnect();
            }

            RUtils.getSemaphore().release();
        } catch (Exception ex) {
            throw new ApplicationException(ExceptionMessages.ERR_20001, ResourceBundleUtils.getString(ExceptionMessages.EXCEPTION_GLOBAL_SYSTEM));
        }
    }

    @Override
    public void execute() {
        buildScript();
    }

    public String getScriptPath() {
        return scriptPath;
    }

    public void setScriptPath(String scriptPath) {
        this.scriptPath = scriptPath;
    }

    public GrrParamDto getGrrParamDto() {
        return grrParamDto;
    }

    public void setGrrParamDto(GrrParamDto grrParamDto) {
        this.grrParamDto = grrParamDto;
    }

    public GrrConfiguration getConfiguration() {
        return configuration;
    }

    public void setConfiguration(GrrConfiguration configuration) {
        this.configuration = configuration;
    }

    public GrrAnalysisDataDto getAnalysisParam() {
        return analysisParam;
    }

    public void setAnalysisParam(GrrAnalysisDataDto analysisParam) {
        this.analysisParam = analysisParam;
    }
}
